package org.asiainfo.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.asiainfo.common.log.annotation.Log;
import org.asiainfo.common.log.enums.BusinessType;
import org.springframework.validation.annotation.Validated;
import org.asiainfo.system.service.SysAnnouncementService;
import org.asiainfo.system.domain.vo.SysAnnouncementVo;
import org.asiainfo.system.domain.bo.SysAnnouncementBo;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.asiainfo.common.mybatis.stream.core.page.PageQuery;
import org.asiainfo.common.mybatis.stream.core.page.TableDataInfo;
import org.asiainfo.common.core.domain.R;
import org.asiainfo.common.web.core.BaseController;
import jakarta.validation.constraints.*;
import org.asiainfo.common.core.validate.AddGroup;
import org.asiainfo.common.core.validate.EditGroup;

import java.util.List;

/**
 * 系统通告表
 *
 * @author dotor-ww
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/sysAnnouncement")
public class SysAnnouncementController extends BaseController {

    private final SysAnnouncementService sysAnnouncementService;

    /**
     * 分页查询
     *
     * @param sysAnnouncementBo 筛选条件
     * @param pageQuery         分页对象
     * @return 查询结果
     */
    @SaCheckPermission("system:sysAnnouncement:list")
    @GetMapping("/list")
    public TableDataInfo<SysAnnouncementVo> list(SysAnnouncementBo sysAnnouncementBo, PageQuery pageQuery) {
        return sysAnnouncementService.queryPageList(sysAnnouncementBo, pageQuery);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    public R<SysAnnouncementVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        return R.ok(sysAnnouncementService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param sysAnnouncementBo 实体
     * @return 新增结果
     */
    @PostMapping
    @SaCheckPermission("system:announcement:add")
    @Log(title = "通知公告", businessType = BusinessType.INSERT)
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysAnnouncementBo sysAnnouncementBo) {
        return toAjax(sysAnnouncementService.insertByBo(sysAnnouncementBo));
    }

    /**
     * 编辑数据
     *
     * @param sysAnnouncementBo 实体
     * @return 编辑结果
     */
    @PutMapping
    @SaCheckPermission("system:announcement:edit")
    @Log(title = "通知公告", businessType = BusinessType.UPDATE)
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysAnnouncementBo sysAnnouncementBo) {
        return toAjax(sysAnnouncementService.updateByBo(sysAnnouncementBo));
    }

    /**
     * 删除数据
     *
     * @param ids 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/{ids}")
    @SaCheckPermission("system:announcement:remove")
    @Log(title = "通知公告", businessType = BusinessType.DELETE)
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(sysAnnouncementService.deleteWithValidByIds(List.of(ids)));
    }

    /**
     * 发布系统通告
     *
     * @param id 主键ID
     * @return 发布是否成功
     */
    @PostMapping("/publish/{id}")
    @SaCheckPermission("system:announcement:publish")
    @Log(title = "通知公告", businessType = BusinessType.UPDATE)
    public R<Void> publish(@NotNull(message = "主键ID不能为空") @PathVariable Long id) {
        return toAjax(sysAnnouncementService.publish(id));
    }

    /**
     * 撤销发布
     *
     * @param id 主键ID
     * @return 撤销是否成功
     */
    @PostMapping("/quash/{id}")
    @SaCheckPermission("system:announcement:quash")
    @Log(title = "通知公告", businessType = BusinessType.UPDATE)
    public R<Void> quash(@NotNull(message = "主键ID不能为空") @PathVariable Long id) {
        return toAjax(sysAnnouncementService.quash(id));
    }

    /**
     * 获取未读消息数量
     *
     * @return 未读消息数量
     */
    @GetMapping("/getUnreadMessageCount")
    public R<Long> getUnreadMessageCount() {
        return R.ok(sysAnnouncementService.getUnreadMessageCount());
    }

    /**
     * 获取未读消息数组信息
     *
     * @return 未读消息数组信息
     */
    @GetMapping("/getUnreadMessageByUserId")
    public R<Object> getUnreadMessageByUserId() {
        return R.ok(sysAnnouncementService.getUnreadMessageByUserId());
    }
}
